package com.zhz.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhz.domain.SysPrivilege;
import com.zhz.domain.SysRole;
import com.zhz.model.R;
import com.zhz.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;

/**
 * @author ：zhz
 * @date ：Created in 2020/12/23
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 * @description: 角色管理
 **/
@RestController
@RequestMapping("/roles")
@Api(tags = "角色管理")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;
    @GetMapping
    @ApiOperation(value = "分页查询角色数据")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "current", value = "当前页"),
                    @ApiImplicitParam(name = "size", value = "每页显示的大小"),
                    @ApiImplicitParam(name = "name", value = "角色名称")
            }
    )
    @PreAuthorize("hasAuthority('sys_role_query')")
    public R<Page<SysRole>> findByPage(@ApiIgnore Page<SysRole> page,String name) {//返回为什么忽略此参数/操作的说明
        page.addOrder(OrderItem.desc("last_update_time"));
        return R.ok(sysRoleService.findByPage(page,name));
    }
    @PostMapping
    @ApiOperation(value = "新增一个角色")
    @PreAuthorize("hasAuthority('sys_role_create')")
            @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "sysPrivilege", value = "sysPrivilege 的json数据")
            }
    )
    public R add(@RequestBody @Validated SysRole sysRole) {
        return sysRoleService.save(sysRole)?R.ok("新增成功"):R.fail("新增失败");
    }
    @PostMapping
    @ApiOperation(value = "删除一个角色")
    @PreAuthorize("hasAuthority('sys_role_delete')")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "ids", value = "要删除的角色id的集合")
            }
    )
    public R delete(@RequestBody String[] ids) {
        if (ids==null || ids.length==0){
            return R.fail("要删除的数据不能为null");
        }
        return sysRoleService.removeByIds(Arrays.asList(ids))?R.ok():R.fail("删除失败");
    }
}
